package view;

import message.LogoutMessage;
import socket.ChessClient;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 游戏大厅主界面.
 *
 * @author 任禹名
 */
public class GameFrame extends JFrame {
    public static final int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
    public static int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;

    private final JDialog degreeDialog = new JDialog();
    private final GameFrame gameFrame = this;

    private final JButton localGameButton = new JButton("本地双人模式");
    private final JButton netGameButton = new JButton("联网模式");
    private final JButton machineGameButton = new JButton("人机模式");
    private final JButton outGameButton = new JButton("退出游戏");
    private final JButton easyButton = new JButton("简单");
    private final JButton normalButton = new JButton("普通");
    private final JButton hardButton = new JButton("困难");

    private final JPanel contentPane = new JPanel() {
        protected void paintComponent(Graphics g) {
            Image image = new ImageIcon("resource/img/home.png").getImage();
            g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        }
    };

    /**
     * GameFrame唯一构造函数.
     */
    public GameFrame() {
        initGameFrame();//初始化主窗口
        initChooseDegreeFrame();//初始化难度选择窗口
        addAction();//为按钮添加监听事件
        this.setVisible(true);
    }

    /**
     * 初始化一个GameFrame对象，GUI的摆放.
     */
    private void initGameFrame() {
        this.setTitle("五子棋");
        this.setSize(1000, 562);
        this.setLocation((screenWidth - 1000) / 2, (screenHeight - 562) / 2);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setIconImage(new ImageIcon("resource/img/logo.png").getImage());
        contentPane.setLayout(null);

        localGameButton.setBounds((int) (this.getWidth() * 0.2), (int) (this.getHeight() * 0.5), this.getWidth() / 6, this.getHeight() / 14);
        netGameButton.setBounds((int) (this.getWidth() * 0.2), (int) (this.getHeight() * 0.6), this.getWidth() / 6, this.getHeight() / 14);
        machineGameButton.setBounds((int) (this.getWidth() * 0.2), (int) (this.getHeight() * 0.7), this.getWidth() / 6, this.getHeight() / 14);
        outGameButton.setBounds((int) (this.getWidth() * 0.2), (int) (this.getHeight() * 0.8), this.getWidth() / 6, this.getHeight() / 14);

        contentPane.add(localGameButton);
        contentPane.add(netGameButton);
        contentPane.add(machineGameButton);
        contentPane.add(outGameButton);

        this.add(contentPane);
    }

    /**
     * 初始化一个JDialog对象，GUI的摆放,改对象用于选择难度.
     */
    private void initChooseDegreeFrame() {
        degreeDialog.setSize(200, 300);
        degreeDialog.setLocation((screenWidth - 200) / 2, (screenHeight - 300) / 2);
        degreeDialog.setLayout(null);

        easyButton.setBounds((int) (degreeDialog.getWidth() * 0.12), (int) (degreeDialog.getHeight() * 0.1), (int) (degreeDialog.getWidth() * 0.7), degreeDialog.getHeight() / 6);
        normalButton.setBounds((int) (degreeDialog.getWidth() * 0.12), (int) (degreeDialog.getHeight() * 0.35), (int) (degreeDialog.getWidth() * 0.7), degreeDialog.getHeight() / 6);
        hardButton.setBounds((int) (degreeDialog.getWidth() * 0.12), (int) (degreeDialog.getHeight() * 0.6), (int) (degreeDialog.getWidth() * 0.7), degreeDialog.getHeight() / 6);

        degreeDialog.add(easyButton);
        degreeDialog.add(normalButton);
        degreeDialog.add(hardButton);

    }

    /**
     * 统一为按钮添加Action.
     *
     * @author 任禹名 张天羿
     */
    private void addAction() {
        localGameButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                toRoom(0);
            }
        });
        netGameButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new LoginDialog(gameFrame);
            }
        });
        machineGameButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                degreeDialog.setVisible(true);
            }
        });
        outGameButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        //不同难度设置
        easyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                degreeDialog.setVisible(false);
                toRoom(1);
            }
        });
        normalButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                degreeDialog.setVisible(false);
                toRoom(2);
            }
        });
        hardButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                degreeDialog.setVisible(false);
                toRoom(3);
            }
        });
        //监听右上角关闭键,如果是线上模式,发送登出报文
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("退出程序");
                if (ChessClient.getInstance().getSocket() != null) {
                    LogoutMessage logoutMessage = new LogoutMessage();
                    ChessClient.getInstance().sendMessage(logoutMessage);
                }
            }
        });
    }

    /**
     * 跳转至游戏房间界面.
     *
     * @param degree 难度
     * @see Room
     */
    private void toRoom(int degree) {
        new Room(this, degree);
        this.setVisible(false);
    }
}
